<?php
// +----------------------------------------------------------------------
// |     【SystemConfig】Service服务类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\SystemConfig as M;
use app\common\validate\SystemConfig as V;

class SystemConfig{
    // 添加
    public static function goAdd($data){
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'add');
            M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 保存，如果没有记录则添加
    public static function goSave($name,$val){
        $row=M::where(['name'=>$name])->find();
        $val=serialize($val);
        try {
            if($row){
                M::where(['id'=>$row['id']])->save(['val'=>$val]);
            }else{
                $data=['name'=>$name,'val'=>$val];
                $data=self::dataFormat($data,'add');
                M::create($data);
            }
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }        
    }
    
    // 编辑
    public static function goEdit($data,$id){
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'edit');
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id){
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id){
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids){
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle(){
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = I('get.limit/d');
        
               //按商家名称查找
               if ($name = I("name")) {
                   $where[] = ["name", "like", "%" . $name . "%"];
               }
               //按公司名查找
               if ($company = I("company")) {
                   $where[] = ["company", "like", "%" . $company . "%"];
               }
               //按电话号码查找
               if ($phone = I("phone")) {
                   $where[] = ["phone", "like", "%" . $phone . "%"];
               }
               //按微信号查找
               if ($weixin = I("weixin")) {
                   $where[] = ["weixin", "like", "%" . $weixin . "%"];
               }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	//修改某个参数
	public static function setFieldValue($data){
		$model =  M::find($data['id']);
		if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
		try{
		    $model->save([
		        $data['field'] => $data['value'],
		    ]);
			return ['msg'=>'操作成功'];
		}catch (\Exception $e){
		    return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
		}
	}
	
	// 预处理添加和修改的数据
	public static function dataFormat($data,$type='add'){
		
		if($type == 'add'){					//添加时才处理
			
		}else if($type == 'edit'){			//修改时才处理

		}
		// 无论修改还是添加都处理
		return $data;
	}
}
